package apollo

import (
	"gf-xb-api/internal/consts"

	"github.com/gogf/gf/contrib/config/apollo/v2"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gctx"
)

func init() {
	var ctx = gctx.GetInitCtx()

	vAppId, err := g.Cfg().Get(ctx, consts.APOLLO_APPID)
	if err != nil {
		g.Log().Fatalf(ctx, `%+v`, err)
	}

	vIp, err := g.Cfg().Get(ctx, consts.APOLLO_IP)
	if err != nil {
		g.Log().Fatalf(ctx, `%+v`, err)
	}

	vCluster, err := g.Cfg().Get(ctx, consts.APOLLO_CLUSTER)
	if err != nil {
		g.Log().Fatalf(ctx, `%+v`, err)
	}

	vNamespace, err := g.Cfg().Get(ctx, consts.APOLLO_GF_NAMESPACE)
	if err != nil {
		g.Log().Fatalf(ctx, `%+v`, err)
	}

	// Create apollo Client that implements gcfg.Adapter.
	adapter, err := apollo.New(ctx, apollo.Config{
		AppID:         vAppId.String(),
		IP:            vIp.String(),
		Cluster:       vCluster.String(),
		NamespaceName: vNamespace.String(),
	})
	if err != nil {
		g.Log().Fatalf(ctx, `%+v`, err)
	}
	// Change the adapter of default configuration instance.
	g.Cfg().SetAdapter(adapter)
}
